package com.qingsong.controller;

import com.mybatisflex.core.paginate.Page;
import com.qingsong.dto.CartInsertDTO;
import com.qingsong.dto.CartPageDTO;
import com.qingsong.dto.CartUpdateDTO;
import com.qingsong.entity.Cart;
import com.qingsong.entity.Cart;
import com.qingsong.vo.CartSimpleListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qingsong.service.CartService;

import java.util.List;

/**
 * 购物车表 控制层。
 *
 * @author qingsong
 * @since 2024-12-22
 */
@Tag(name = "购物车")
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "新增-单条", description = "新增一条评论记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody CartInsertDTO dto) {
        return cartService.insert(dto);
    }

    @Operation(summary = "查询-单条", description = "按主键查询一条评论记录")
    @GetMapping("select/{id}")
    public Cart select(@PathVariable("id") Long id) {
        return cartService.select(id);
    }

    @Operation(summary = "查询-简单列表", description = "查询全部评论记录,仅返回简单信息")
    @GetMapping("simpleList")
    public List<CartSimpleListVO> simpleList() {
        return cartService.simpleList();
    }

    @Operation(summary = "查询-分页", description = "分页查询评论记录")
    @GetMapping("page")
    public Page<Cart> page(@Validated CartPageDTO dto) {
        return cartService.page(dto);
    }

    @Operation(summary = "修改-单条", description = "按主键修改一条评论记录")
    @PutMapping("update")
    public boolean update(@RequestBody CartUpdateDTO dto) {
        return cartService.update(dto);
    }

    @Operation(summary = "删除-清空记录", description = "按用户ID清空购物车")
    @DeleteMapping("clearByUserId/{userId}")
    public boolean clearByUserId(@PathVariable("userId") Long userId) {
        return cartService.cleanByUserId(userId);
    }

    @Operation(summary = "查询购物车总价", description = "按用户ID查询购物车总价")
    @GetMapping("totalAmountByUserId/{userId}")
    public double totalAmountByUserId(@PathVariable("userId") Long userId) {
        return cartService.totalAmountByUserId(userId);
    }

    @Operation(summary = "删除-单条", description = "按主键删除一条评论记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return cartService.delete(id);
    }

    @Operation(summary = "删除-批量", description = "按主键批量删除评论记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return cartService.deleteBatch(ids);
    }

}
